package com.jsy.imagepicker;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;

import java.io.Serializable;

public class ImagePicker {
    /**
     * 可配置参数
     */
    public static class Parameters implements Serializable {
        public int mMaxCount = 1;//要选择的图片最大张数
        public int mSpanCount = 3;//显示几列，默认值
        public boolean mUseCamera = true;//是否使用摄像头,默认使用
        public boolean mFilterGif = false;//是否过滤GIF图，默认不过滤
        public boolean mCut = false;//是否需要裁剪(仅限选择单张图时支持)
        public int mAspectX = 1;//需要裁剪的宽度比例
        public int mAspectY = 1;//需要裁剪的高度比例
        public int mCutWidth = 720;    //裁剪后的图片输出宽度
        public int mCutHeight = 720;   //裁剪后的图片输出高度
    }


    Parameters parameters = new Parameters();


    /**
     * 此方法为举例，供复制后自行修改
     *
     * @param activity
     */
   public static void test(Activity activity) {
        ImagePicker.from(activity)
                .setMaxCount(1)         //设置选择图片最大数量，默认为1
                .setUseCamera(true)     //是否支持摄像头拍照，默认为支持
                .setSpanCount(3)        //设置选图界面按几列显示，默认为4列
                .setFilterGif(true)     //是否过滤gif图片，默认不过滤
                .setCut(true)           //是否需要裁剪，默认不剪切（只支持选择一张图时使用）
                .setCutAspect(1,1)          //设置需要裁剪，并设置裁剪比例（只支持选择一张图时使用）
                .setCutSize(720,720)    //设置需要裁剪，并设置剪切输出图片尺寸（只支持选择一张图时使用）
                .start(1);
    }


    Activity mActivity;
    Fragment mFragment;
    android.app.Fragment mAppFragment;
    Context context;

    public static ImagePicker from(Activity activity) {
        ImagePicker ip = new ImagePicker();
        ip.mActivity = activity;
        ip.context = activity;
        return ip;
    }

    public ImagePicker from(Fragment fragment) {
        mFragment = fragment;
        context = fragment.getContext();
        return this;
    }

    public ImagePicker from(android.app.Fragment fragment) {
        mAppFragment = fragment;
        context = fragment.getActivity();
        return this;
    }

    /**
     * 预选图片数量
     * @param maxCount
     * @return
     */
    public ImagePicker setMaxCount(int maxCount) {
        parameters.mMaxCount = maxCount;
        return this;
    }

    /**
     * 展示图片列表时，一行几张图
     * @param spanCount
     * @return
     */
    public ImagePicker setSpanCount(int spanCount) {
        parameters.mSpanCount = spanCount;
        return this;
    }

    /**
     * 是否支持摄像头
     * @param useCamera
     * @return
     */
    public ImagePicker setUseCamera(boolean useCamera) {
        parameters.mUseCamera = useCamera;
        return this;
    }

    /**
     * 是否过滤GIF图
     * @param filterGif
     * @return
     */
    public ImagePicker setFilterGif(boolean filterGif) {
        parameters.mFilterGif = filterGif;
        return this;
    }

    /**
     * 是否需要裁剪（仅支持单张图选择）
     * @param cut
     * @return
     */
    public ImagePicker setCut(boolean cut) {
        parameters.mCut = cut;
        return this;
    }

    /**
     * 图片裁剪的输出图片尺寸
     * @param cutWidth
     * @param cutHeight
     * @return
     */
    public ImagePicker setCutSize(int cutWidth,int cutHeight) {
        parameters.mCut = true;
        parameters.mCutWidth = cutWidth;
        parameters.mCutHeight = cutHeight;
        return this;
    }

    /**
     * 图片裁剪的宽高比例
     * @param aspectX
     * @param aspectY
     * @return
     */
    public ImagePicker setCutAspect(int aspectX,int aspectY) {
        parameters.mCut = true;
        parameters.mAspectX = aspectX;
        parameters.mAspectY = aspectY;
        return this;
    }


    public void start(int requestCode) {
        Intent intent = new Intent(context, ImagePickerActivity.class)
                .putExtra("parameters", parameters);


        if (mActivity != null) {
            mActivity.startActivityForResult(intent, requestCode);
        } else if (mFragment != null) {
            mFragment.startActivityForResult(intent, requestCode);
        } else if (mAppFragment != null) {
            mAppFragment.startActivityForResult(intent, requestCode);
        }
    }

}
